<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Receivepurmat"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.TobDAO" %>
<%@page import="db.Tob" %>
<%@page import="db.TeamDAO" %>
<%@page import="db.Team" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="db.Supplier" %>
<%@page import="db.RetreqDAO" %>
<%@page import="db.User"%>
<%@page import="db.UserDAO"%>
<%@page import="db.Group"%>
<%@page import="db.OutreqDAO"%>
<%@page import="db.Outreq"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Retmat"%>
<%@page import="db.RetmatDAO"%>
<%@page import="db.Retreq"%>
<%@page import="db.Outpurmat"%>
<%@page import="db.OutpurmatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceivepurmatDAO receivepurmatDAO = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	OutpurmatDAO outpurmatDAO = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	RetmatDAO retmatDAO = (RetmatDAO)ctx.getBean("RetmatDAO");
	RetreqDAO retreqDAO = (RetreqDAO)ctx.getBean("RetreqDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	OutreqDAO outreqDAO = (OutreqDAO)ctx.getBean("OutreqDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	String pid = request.getParameter("pid");
	List list =new ArrayList();
	String message = "";
	boolean success = false;
	if(pid!=null){//确认有勾选物资
		//更新审批状态
		Retreq retreq = retreqDAO.findById(Integer.parseInt(pid));
		int userid = (Integer)session.getAttribute("id");
		retreq.setOkUserId(userid);
		retreq.setOkState(3);//不通过
		retreq.setOkDate(new Date());
		//查询pid下的所有物质
		list = retmatDAO.findByRetmatIdAndStateDel(Integer.parseInt(pid),1);
		for(int i=0;i<list.size();i++){
			Retmat retmat = (Retmat)list.get(i);
			if(retmat!=null){
				int rnum = retmat.getRetNum();//申请退数
				retmat.setState(2);//无效状态
				Outpurmat outpurmat= retmat.getOutpurmat();
				int un = outpurmat.getRetUseNum();
				int tui = un+rnum;
				outpurmat.setRetUseNum(tui);
				retmatDAO.attachDirty(retmat);
				outpurmatDAO.attachDirty(outpurmat);
			}
			
		}
		retreqDAO.attachDirty(retreq);
		success = true;
		message = "恭喜您！操作成功！";
	}else{
		message = "操作失败！";
		success = false;
	}
	request.setAttribute("success",success);
	request.setAttribute("message",message);
	pageContext.forward("/vm/returnStock/retAppmessage1.vm");
%>
<%!
	public List getListReMap(List list){
		List listmap = new ArrayList();
		//System.out.println("===============>>>>>>>>");
		if(list != null && list.size()>0){
		
			for(int i=0;i<list.size();i++){
			
				Map map = new HashMap();
				Object[] aa= (Object[]) list.get(i);
				map.put("pid", aa[0]);
				map.put("pstate", aa[1]);
				map.put("pcode", aa[2]);
				map.put("procode", aa[3]);
				map.put("proname", aa[4]);
				SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); 
				if(aa[5]!=null){
					map.put("pdate", sf.format(aa[5]));
				}
				
				if(aa[6]!=null){
					map.put("penddate", sf.format(aa[6]));
				}
				
				listmap.add(map);
			}
		}
		return listmap;
	}
	
%>